<?php


namespace app\home\controller\login;

use app\common\model\HomeTokenModel;
use app\common\model\UserModel;
use app\common\model\UserSet;
use app\home\controller\Base;
use app\common\model\UserModel as models;
use app\validate\home\Login as validates;
use think\exception\ValidateException;

class UpdatePwd extends Base
{
    protected $model;

    /**
     * 修改密码
     */
    public function initialize()
    {
        $this->model = new models();
        parent::initialize(); // TODO: Change the autogenerated stub
    }

    //修改密码和手机号 获取用户信息在修改密码
    public function change_pwd(): string
    {
        //过滤数据
        $postField = 'phone,pwd';
        $post = $this->request->only(explode(',', $postField), 'post', null);
        if (empty($post['phone'])) return show([], 0, '手机号必填');
        $post = array_filter($post);//过滤为空的数据
        $post['user_name'] = $post['phone'];//手机号就是账号
        //查询手机号是否存在
        $find = $this->model->where('phone', $post['phone'])->find();
        if ($find) return show([], config('ToConfig.http_code.error'), '手机号已经存在');
        //密码加密
        if (isset($post['pwd'])) $post['pwd'] = pwdEncryption($post['pwd']);
        //执行修改
        $save = $this->model->where('id', self::$user['id'])->update($post);
        (new HomeTokenModel())->where('user_id',self::$user['id'])->delete();
        if ($save) return show();
        return show([], config('ToConfig.http_code.error'), '修改密码失败');

    }

    //绑定手机号
    public function change_phone(): string
    {
        $phone = $this->request->param('phone', 0);
        if (strlen($phone) < 11) return show([], config('ToConfig.http_code.error'), '号码不正确');

        $find = UserModel::where('phone', $phone)->find();
        if ($find) return show([], config('ToConfig.http_code.error'), '号码已存在');

        $save = $this->model->where('id', self::$user['id'])->update(['phone' => $phone]);
        if ($save) return show();
        return show([], config('ToConfig.http_code.error'), '绑定失败');
    }

    //忘记密码
    public function forget(): string
    {
        //过滤数据
        $postField = 'phone,pwd';
        $post = $this->request->only(explode(',', $postField), 'post', null);

        //验证数据
        try {
            validate(validates::class)->scene('forget')->check($post);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return show([], config('ToConfig.http_code.error'), $e->getError());
        }

        //判断输入账号是否正确
        if ($post['phone'] != self::$user['phone']) return $this->failed('输入账号和登陆的不匹配');
        //插入数据
        $find = $this->model->where(['phone' => $post['phone']])
            ->save(['pwd' => pwdEncryption($post['pwd'])]);
        if (empty($find)) return show([], config('ToConfig.http_code.error'), '修改密码失败');
        return show();
    }

    //用户修改筹码
    public function update_chip(): string
    {
        //过滤数据
        $postField = 'chip';
        $post = $this->request->only(explode(',', $postField), 'post', null);
        if (empty($post)) return show([], config('ToConfig.http_code.error'), '参数错误');
        $userSet = new UserSet();
        $userSet->where('u_id',self::$user['id'])->update(['user_chip'=>json_encode($post)]);
        return show();
    }
}